package com.yu.yuapi.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yu.yuapi.annotation.AuthCheck;
import com.yu.yuapi.common.BaseResponse;
import com.yu.yuapi.common.ErrorCode;
import com.yu.yuapi.common.ResultUtils;
import com.yu.yuapi.constant.UserConstant;
import com.yu.yuapi.exception.BusinessException;
import com.yu.yuapi.exception.ThrowUtils;
import com.yu.yuapi.model.dto.user.UserAddRequest;
import com.yu.yuapi.model.dto.userinterfaceinfo.UserInterfaceInfoAddRequest;
import com.yu.yuapi.model.dto.userinterfaceinfo.UserInterfaceInfoQueryRequest;
import com.yu.yuapi.model.dto.userinterfaceinfo.UserInterfaceInfoUpdateRequest;
import com.yu.yuapi.model.entity.User;
import com.yu.yuapi.model.entity.UserInterfaceInfo;
import com.yu.yuapi.service.UserInterfaceInfoService;
import org.springframework.beans.BeanUtils;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import static com.yu.yuapi.service.impl.UserServiceImpl.SALT;

/**
 * 用户调用接口关系
 *
 * @author yu
 */
@RestController
@RequestMapping("/userInterfaceInfo")
public class UserInterfaceInfoController {
    @Resource
    private UserInterfaceInfoService userInterfaceInfoService;

    @PostMapping("/add")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Long> addUserInterfaceInfo(@RequestBody UserInterfaceInfoAddRequest userInterfaceInfoAddRequest) {
        if (userInterfaceInfoAddRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        UserInterfaceInfo userInterfaceInfo = new UserInterfaceInfo();
        userInterfaceInfoService.validUserInterfaceInfo(userInterfaceInfo, true);
        BeanUtils.copyProperties(userInterfaceInfoAddRequest, userInterfaceInfo);
        userInterfaceInfoService.save(userInterfaceInfo);
        return ResultUtils.success(userInterfaceInfo.getId());
    }

    @PostMapping("/update")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> updateUserInterfaceInfo(@RequestBody UserInterfaceInfoUpdateRequest userInterfaceInfoUpdateRequest) {
        if (userInterfaceInfoUpdateRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        UserInterfaceInfo userInterfaceInfo = new UserInterfaceInfo();
        BeanUtils.copyProperties(userInterfaceInfoUpdateRequest, userInterfaceInfo);
        userInterfaceInfoService.validUserInterfaceInfo(userInterfaceInfo, false);
        boolean result = userInterfaceInfoService.updateById(userInterfaceInfo);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR, "更新失败");
        return ResultUtils.success(result);
    }

    @PostMapping("/list/page")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Page<UserInterfaceInfo>> pageUserInterfaceInfo(@RequestBody UserInterfaceInfoQueryRequest userInterfaceInfoQueryRequest) {
        if (userInterfaceInfoQueryRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        UserInterfaceInfo userInterfaceInfo = new UserInterfaceInfo();
        BeanUtils.copyProperties(userInterfaceInfoQueryRequest, userInterfaceInfo);
        QueryWrapper<UserInterfaceInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(userInterfaceInfo.getUserId() != null, "userId", userInterfaceInfo.getUserId());
        queryWrapper.eq(userInterfaceInfo.getInterfaceInfoId() != null, "interfaceInfoId", userInterfaceInfo.getInterfaceInfoId());
        Page<UserInterfaceInfo> page = new Page<>(userInterfaceInfoQueryRequest.getCurrent(), userInterfaceInfoQueryRequest.getPageSize());
        Page<UserInterfaceInfo> paged = userInterfaceInfoService.page(page, queryWrapper);
        return ResultUtils.success(paged);
    }
}